package Controller;

import Model.BangDiem;
import Model.KhoaHoc;
import Model.LopHoc;
import Model.MonHoc;
import Model.SinhVien;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Nhóm java lớp PNV2T
* 				- Nguyễn Thị Lệ Đài
* 				- Nguyễn Chí Hướng
* 
* Contain all edit function of application
*/
public class Insert {
    private ConnectDB con = null;
    private ResultSet rs = null;
    /** Creates a new instance of Insert */
    public Insert() {
        try {
            con = new ConnectDB();
            con.open();
        } catch (Exception ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }
    
    public int ThemKhoaMoi(KhoaHoc newKH){
        String sql = "Insert Into KhoaHoc Values(N'"+newKH.getTenKhoa()+"','"+newKH.getBatDau()+"')";
        con.setMode(ConnectDB.MODE_NORMAL);
        try {
            return con.doUpdate(sql);
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
        return 0;
    }
    
    public int ThemLopHoc(LopHoc newLop){
        String sql = "Insert Into LopHoc Values(?,?,?,?,?)";
        con.setMode(ConnectDB.MODE_PREPARED);
        try {
            con.setStatement(sql);
            con.setString(1,newLop.getTenLop());
            con.setInt(2,newLop.getMaKhoa());
            con.setString(3,newLop.getHeDaoTao());
            con.setString(4,newLop.getChuNhiem());
            con.setString(5,newLop.getLopTruong());
            return con.doUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
        return 0;
    }
    
    public int ThemSinhVien(SinhVien newSV){
        String sql = "Insert Into SinhVien Values(?,?,?,?,?,?,?)";
        con.setMode(ConnectDB.MODE_PREPARED);
        try {
            con.setStatement(sql);
            con.setString(1,newSV.getMaSV());
            con.setInt(2,newSV.getMaLop());
            con.setString(3,newSV.getTen());
            con.setString(4,newSV.getQueQuan());
            con.setString(5,newSV.getNgaySinh());
            con.setString(6,newSV.getGioiTinh());
            con.setString(7,newSV.getTonGiao());
            return con.doUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
        return 0;
    }
    
    public int ThemDiem(BangDiem newDiem){
        String sql = "Insert Into KetQuaHocTap Values(?,?,?,?,?,?)";
        con.setMode(ConnectDB.MODE_PREPARED);
        try {
            con.setStatement(sql);
            con.setInt(1,newDiem.getMaSV());
            con.setInt(2,newDiem.getMaMon());
            con.setDouble(3,newDiem.getDiemKiemTra());
            con.setDouble(4,newDiem.getDiemChuyenCan());
            con.setDouble(5,newDiem.getDiemThi());
            con.setDouble(6,newDiem.getDiemTongKet());            
            return con.doUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
        return 0;
    }
    
    public int ThemMonHoc(MonHoc newMon){
        String sql = "Insert Into MonHoc Values(?,?,?,?)";
        con.setMode(ConnectDB.MODE_PREPARED);
        try {
            con.setStatement(sql);
            con.setString(1,newMon.getTenMon());
            con.setInt(2,newMon.getKyHoc());
            con.setInt(3,newMon.getNamHoc());
            con.setInt(4,newMon.getSoTinChi());
            return con.doUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
        return 0;
    }
    
    public int ThemTaiKhoan(String tenTK, String mK) {
        String sql = "Insert Into Users Values(?,?)";
        con.setMode(ConnectDB.MODE_PREPARED);
        try {
            con.setStatement(sql);
            con.setString(1,tenTK);
            con.setString(2,mK);
            return con.doUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
        return 0;
    }
    
    public void CloseData(){
        try {
            con.close();
        } catch (Exception ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }
}
